﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.IService;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework.Model;
using NHibernate;
using FantastPlatform.Framework.Common;
using FantastPlatform.Framework.Exception;

namespace FantastPlatform.Framework.Service
{
    internal class PositionService : DataService<IPosition, Position>, IPositionService
    {
        public override void Delete(int id)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tan) =>
            {
                IPosition entity = Session.Get<Position>(id) as IPosition;
                if (entity != null)
                    Check.Require<InvalidCodeException>(!IsUsing(entity)||CollectionHelper.IsNullOrEmpty<IPost>(entity.Posts.Where(c=>c.Enable==true).ToList<IPost>()), "当前职位正在被使用不能删除！");
                    Session.Delete(entity);
                    if (IsUsing(entity))
                    {
                        entity.Enable = false;
                        Session.Update(entity);
                    }
                    else
                    {
                        Session.Delete(entity);
                    }
            }, transaction);
        }
        public override void Delete(List<int> ids)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tan) =>
            {
                foreach (int id in ids)
                {
                    IPosition entity = Session.Get<Position>(id) as IPosition;
                    if (entity != null)
                        Check.Require<InvalidCodeException>(!IsUsing(entity) || CollectionHelper.IsNullOrEmpty<IPost>(entity.Posts.Where(c => c.Enable == true).ToList<IPost>()), "当前职位正在被使用不能删除！");
                    Session.Delete(entity);
                    if (IsUsing(entity))
                    {
                        entity.Enable = false;
                        Session.Update(entity);
                    }
                    else
                    {
                        Session.Delete(entity);
                    }
                }
            }, transaction);
        }
    }
}
